package com.wtwd.campus.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wtwd.campus.common.Constants;
import com.wtwd.campus.common.RespEntity;
import com.wtwd.campus.dao.LogMapper;
import com.wtwd.campus.entity.*;
import com.wtwd.campus.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class LogServiceImpl implements LogService {
    @Autowired
    private LogMapper logMapper;

    @Override
    public Map<String, Object> getCallRecord(QueryCallRecordDataVo data) {
        //构建分页
        Page<Map<String, Object>> page = new Page<>(data.getCurrPage(), data.getPageSize());
        IPage<Map<String, Object>> iPage = logMapper.getCallRecord(data, page);
        //处理数据
        Map<String, Object> result = disposalData(iPage);
        return result;
    }

    @Override
    public Map<String, Object> getMonitorRecord(QueryMonitorRecordDataVo data) {
        //构建分页
        Page<Map<String, Object>> page = new Page<>(data.getCurrPage(), data.getPageSize());
        IPage<Map<String, Object>> iPage = logMapper.getMonitorRecord(data, page);
        //处理数据
        Map<String, Object> result = disposalData(iPage);
        return result;
    }

    @Override
    public Integer addAuditLog(AuditLog auditLog) {
        return logMapper.addAuditLog(auditLog);
    }

    @Override
    public List<String> getOperationModule(String orgCode) {
        return logMapper.getOperationModule(orgCode);
    }

    @Override
    public Map<String, Object> getOperationRecordList(QueryOperationRecordVo data) {
        //构建分页
        Page<Map<String, Object>> page = new Page<>(data.getCurrPage(), data.getPageSize());
        IPage<Map<String, Object>> result = logMapper.getOperationRecordList(data, page);
        return disposalData(result);
    }

    @Override
    public String getOrgCodeBySchoolId(Integer schoolId) {
        return logMapper.getOrgCodeBySchoolId(schoolId);
    }

    @Override
    public Map<String, Object> getOrgCodeAndStuNameByStuId(Integer stuId) {
        return logMapper.getOrgCodeAndStuNameByStuId(stuId);
    }

    @Override
    public Integer addCallRecord(CallRecord record) {
        return logMapper.addCallRecord(record);
    }

    @Override
    public Integer addMonitorRecord(MonitorRecord record) {
        return logMapper.addMonitorRecord(record);
    }

    @Override
    public Map<String, Object> getCommandRecordList(Page<Object> page, HashMap<String, Object> searchMap, String orgCode) {
        IPage<Map<String, Object>> commandRecordList = logMapper.getCommandRecordList(searchMap, orgCode, page);
        List<Map<String, Object>> records = commandRecordList.getRecords();
        if (records!=null&&records.size()>0){
            for (Map<String, Object> map : records) {
                if (map.get("user_id").equals(-1)){
                    map.put("account","微信用户");
                }else if (map.get("user_id").equals(-2)){
                    map.put("account","H5用户");
                }else if(map.get("user_id").equals(0)){
                    map.put("account","其他客户");
                }
                //处理时间
                if (Constants.COMMAND_WAITING_SEND==(Integer)map.get("result")){//如果是待发送时间，转化为指令发送时间
                    map.put("begin_time",map.get("create_time"));
                }

            }
        }
        HashMap<String, Object> restMap = new HashMap<>();
        restMap.put("total",commandRecordList.getTotal());
        restMap.put("date",records);
        return restMap;
    }

    @Override
    public Map<String, Object> getAlarmRecordList(String orgCode, HashMap<String, Object> searchMap, Page<Object> page) {
        IPage<Map<String, Object>> data = logMapper.getAlarmRecordList(orgCode, searchMap, page);
        return disposalData(data);
    }

    @Override
    public Map<String, Object> getLoginLogByCondition(String orgCode, String name, Long startTime, Long endTime, Page<Object> page) {
        IPage<Map<String, Object>> data = logMapper.getLoginLogByCondition(orgCode, name, startTime, endTime, page);
        return disposalData(data);
    }

    @Override
    public School getSchoolById(Integer schoolId) {
        return logMapper.getSchoolById(schoolId);
    }

    @Override
    public Map<String, Object> getSchoolAndGradeByGradeId(Integer gradeId) {
        return logMapper.getSchoolAndGradeByGradeId(gradeId);
    }

    @Override
    public Map<String, Object> getClassMessAndSchoolCodeById(Integer classId) {
        return logMapper.getClassMessAndSchoolCodeById(classId);
    }

    @Override
    public Map<String, Object> getTeacherNameAndSchoolCodeById(Integer teaId) {
        return logMapper.getTeacherNameAndSchoolCodeById(teaId);
    }

    @Override
    public List<DeviceSOSList> getSOSListByDeviceId(Integer deviceId) {
        return logMapper.getSOSListByDeviceId(deviceId);
    }

    @Override
    public Integer updateSosListStatus(Integer id, Integer status, String commandNumber) {
        return logMapper.updateSosListStatus(id, status, commandNumber);
    }

    @Override
    public DeviceSOSList getDeviceSOSMessById(Integer id) {
        return logMapper.getDeviceSOSMessById(id);
    }

    @Override
    public Integer insertSosNumberList(DeviceSOSList data) {
        return logMapper.insertSosNumberList(data);
    }

    /**
     * 构建返回数据
     *
     * @return
     */
    public Map<String, Object> disposalData(IPage<Map<String, Object>> iPage) {
        Map<String, Object> map = new HashMap<>();
        List<Map<String, Object>> records = iPage.getRecords();
        long total = iPage.getTotal();
        map.put("total", total);
        map.put("list", records);
        return map;
    }

    //分页
    public <T> Map<String, Object> disposalData(List<T> list, Integer curPage, Integer pageSize) {
        if (curPage <= 0) {
            curPage = 1;
        }
        List<T> resultRecord = new ArrayList<>();
        Integer total = list.size();
        int start = (curPage - 1) * pageSize;
        int end = start + pageSize;
        if (list.size() < start) {
            return null;
        }
        for (int i = start; i < (list.size() < end ? list.size() : end); i++) {
            resultRecord.add(list.get(i));
        }
        Map<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("date", resultRecord);
        return map;
    }

    @Override
    public List<Map<String, Object>> getCallRecordByDeviceId(Integer deviceId) {
        return logMapper.getCallRecordByDeviceId(deviceId);
    }

    @Override
    public List<Map<String,Object>> getCallRecordByStuId(Integer stuId,Integer num) {
        Integer start=num*10;
        Integer end=10;
        return logMapper.getCallRecordByStuId(stuId,start,end);
    }

    @Override
    public List<Map<String,Object>> getWarningTypeByDeviceId(Integer deviceId) {
        return logMapper.getWarningTypeByDeviceId(deviceId);
    }

    @Override
    public List<Map<String,Object>> getWarningListByStuIdAndType(Integer stuId, Integer type,Integer num) {
        Integer start=num*10;
        Integer end=10;
        return logMapper.getWarningListByStuIdAndType(stuId, type,start,end);
    }

    @Override
    public Integer updateSosListData(SOSListVO data,Integer status,String commandNumber) {
        return logMapper.updateSosListData(data, status,commandNumber);
    }

    @Override
    public int selectCountByTypeAndTime(Integer deviceId,Integer type, Long start) {
        return logMapper.selectCountByTypeAndTime(deviceId, type, start);
    }

    @Override
    public List<Map<String,Object>> getWarningListByDeviceIdAndType(Integer deviceId, Integer type) {
        return logMapper.getWarningListByDeviceIdAndType(deviceId, type);
    }

    @Override
    public List<Map<String,Object>> getWarningTypeByStuId(Integer stuId) {
        return logMapper.getWarningTypeByStuId(stuId);
    }
}
